//
//  LEEditPasswordController.m
//  LoveExpressBusiness
//
//  Created by 刘甲奇 on 2017/5/11.
//  Copyright © 2017年 DFH. All rights reserved.
//

#import "LEEditPasswordController.h"
#import "MakerTools.h"
#import "NSString+MD5.h"
#import <SMS_SDK/SMSSDK.h>

@interface LEEditPasswordController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *codeField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField1;
@property (weak, nonatomic) IBOutlet UIButton *showButton;
@property (weak, nonatomic) IBOutlet UIButton *showButton1;
@property (weak, nonatomic) IBOutlet UIButton *getCodeButton;

@property(nonatomic,strong)NSTimer *timer;
@property(nonatomic,assign)NSInteger second;

@end

@implementation LEEditPasswordController

- (void)setupUI
{
    [super setupUI];
    self.view.backgroundColor = LEBackgroundColor;
    self.title = @"更改密码";
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveClick:)];
    self.navigationItem.rightBarButtonItem = rightItem;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.timer invalidate];
    self.timer = nil;
}
#pragma mark- 网络请求
- (void)requestSavePassword
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    NSString *oldPassword = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
    NSString *paramString = [NSString stringWithFormat:@"password|%@,new_password|%@",[oldPassword stringToMD5],[self.passwordField.text stringToMD5]];
    [dict setObject:paramString forKey:@"params"];

    [SVProgressHUD showWithStatus:@"正在保存中"];
    __weak typeof (self) weakSelf = self;
    [API requestPostAppendKey:OwnResetPasswordUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            NSString *error = datas[@"error"];
            if (error)
            {
                [SVProgressHUD showErrorWithStatus:error];
            }
        }else if ([datas isKindOfClass:[NSString class]])
        {
            [SVProgressHUD showSuccessWithStatus:datas];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:@"网络错误"];

    }];
}
#pragma mark- 点击事件
- (void)saveClick:(UIButton *)sender
{
    LELog(@"%s---%d---保存",__func__,__LINE__);
    [self.passwordField1 resignFirstResponder];
    [self checkCode];
}
- (IBAction)getCodeClick:(UIButton *)sender {
    if ([MakerTools isPhoneNumber:self.phoneField.text]) {
        self.getCodeButton.enabled = NO;
        [self requestCode];
    }else
    {
        [SVProgressHUD showInfoWithStatus:@"请输入正确的手机号"];
    }
    
}

- (IBAction)showClick:(UIButton *)sender {
    [sender setSelected:!sender.selected];
    if (sender.selected) {
        UIImage *image = [UIImage imageNamed:@"editpassword_off"];
        self.passwordField.secureTextEntry = YES;
        [sender setImage:image forState:0];
    }else{
        UIImage *image = [UIImage imageNamed:@"editpassword_on"];
        self.passwordField.secureTextEntry = NO;
        [sender setImage:image forState:0];
    }
    
}
- (IBAction)showClick1:(UIButton *)sender {
    [sender setSelected:!sender.selected];
    if (sender.selected) {
        UIImage *image = [UIImage imageNamed:@"editpassword_off"];
        self.passwordField1.secureTextEntry = YES;
        [sender setImage:image forState:0];
    }else{
        UIImage *image = [UIImage imageNamed:@"editpassword_on"];
        self.passwordField1.secureTextEntry = NO;
        [sender setImage:image forState:0];
    }

}
#pragma mark- privateMethod
/**
 初始化 timer
 */
- (void)initTimer
{
    self.second = 60;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerCallBack) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
}
- (void)timerCallBack
{
    self.second--;
    [self.getCodeButton setTitle:[NSString stringWithFormat:@"%ld s",_second] forState:0];
    if (_second == 0) {
        self.getCodeButton.enabled = YES;
        [self.getCodeButton setTitle:@"重新获取" forState:0];
        [self.timer invalidate];
        self.timer = nil;
        return ;
    }
}

/**
 获取验证码
 */
- (void)requestCode
{
     __weak typeof (self) weakSelf = self;
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneField.text zone:@"86" result:^(NSError *error) {
        if (!error) {
            [SVProgressHUD showInfoWithStatus:@"获取验证码成功"];
            [weakSelf initTimer];
        }else{
            LELog(@"%s---%d---%@",__func__,__LINE__,error);
            [SVProgressHUD showErrorWithStatus:@"获取验证码失败"];
        }
    }];
}

/**
 验证验证码
 */
- (void)checkCode
{
    if ([self checkEmpty]) {
        return;
    }
     __weak typeof (self) weakSelf = self;
    [SMSSDK commitVerificationCode:self.codeField.text phoneNumber:self.phoneField.text zone:@"86" result:^(NSError *error) {
        if (!error)
        {
            NSLog(@"验证成功");
            [weakSelf checkPassword];
        }
        else
        {
            [SVProgressHUD showInfoWithStatus:@"验证码验证失败"];
        }
    }];
}

/**
 验证是够为空等
 */
- (BOOL)checkEmpty
{
    if (![MakerTools isPhoneNumber:self.phoneField.text]) {
        [SVProgressHUD showInfoWithStatus:@"输入正确手机号"];
        return YES;
    }
    if ([self.codeField.text isEqualToString:@""]) {
        [SVProgressHUD showInfoWithStatus:@"验证码不能为空"];
        return YES;
    }
    return NO;
}

/**
 检验密码
 */
- (void)checkPassword
{
    if (self.passwordField.text.length < 6 || self.passwordField1.text.length <6)
    {
        [SVProgressHUD showInfoWithStatus:@"密码不能小于6位"];
        return;
    }
    if (![self.passwordField1.text isEqualToString:self.passwordField.text]) {
        [SVProgressHUD showInfoWithStatus:@"两次输入不相同"];
        return;
    }
    [self requestSavePassword];
}
@end






